<template>
  <div class="main">
    <div class="m-search">
      <el-input placeholder="请输入用户名" v-model="username" @change="searchUser" class="m-s-input"></el-input>
      <el-button @click="searchUser" type="primary" class="m-s-btn">查找</el-button>
      <el-button @click="newUser" type="primary" class="m-s-btn" v-if="currentUser.role === 1">新增</el-button>
    </div>
    <div class="m-table">
      <el-table :data="tableData"  style="width: 100%">
        <el-table-column
          prop="name"
          label="用户名"
          >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          >
        </el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          >
        </el-table-column>
        <el-table-column
          prop="lastLoginTime"
          label="上次登录时间"
          >
        </el-table-column>
        <el-table-column
          label="操作"
          width="250"
        >
          <template slot-scope="scope">
            <el-button @click="editUser(scope.row)" type="primary" size="small" v-if="currentUser.role === 1">编辑</el-button>
            <el-button @click="deleteUser(scope.row)" type="danger" size="small" v-if="currentUser.role === 1">删除</el-button>
            <el-button @click="resetPassword(scope.row)" type="danger" size="small" v-if="currentUser.role === 1">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getUserListByName ,resetPasswordById ,deleteUserById} from '@/api/user'
export default {
  data(){
    return {
      username:'',
      tableData:[],
      currentUser:{}
    }
  },
  
  created(){
    console.log('created:')
    this.searchUser()
    this.currentUser = this.$store.getters.currentUser
  },
  methods:{
    async searchUser(){
      let res = await getUserListByName({ name : this.username })
      this.tableData = res.data
    },
    newUser(){
      this.$router.push('/setting/newUser')
    },
    editUser(row){
      this.$router.push({path:'/setting/newUser',query:{ id : encodeURIComponent(row.id)}})
    },
    deleteUser(row){
      this.$confirm('确定要删除该用户吗？','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async ()=>{
        let res = await deleteUserById({id:row.id})
        this.$message({
          type:'success',
          message:'删除成功'
        })
        this.searchUser()
      })
    },
    resetPassword(row){
      this.$confirm('确定要重置该用户密码吗？','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async ()=>{
        let res = await resetPasswordById({id:row.id})
        if(res.code === 0){
          this.$message({
            type:'success',
            message:'密码已经重置为123456'
          })
        }else{
          this.$message({
            type:'error',
            message:'重置密码失败'
          })
        }
      })
    }
  }
  
}
</script>
<style lang="scss" scoped>
.main{
  padding:20px;
  overflow: hidden;
  .m-search{
    .m-s-input{
      width: 300px;
    }
    .m-s-btn{
      margin-left: 30px;
    }
  }
  .m-table{
    margin-top:20px;
  }
}
</style>


